package com.agrm.rabc.service.impl;


import com.agrm.model.rabc.dto.PermResourceMapping;
import com.agrm.rabc.mapper.PermResourceMappingDao;
import com.agrm.rabc.service.PermResourceMappingService;
import com.agrm.rabc.service.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;


@CacheConfig(cacheNames = "c.s.r.s.impl.PermResourceMappingServiceImpl")
@Service
public class PermResourceMappingServiceImpl implements PermResourceMappingService {
    private PermResourceMappingDao permResourceMappingDao;
    private ResourceService resourceService;

    @Autowired
    public void setPermResourceMappingDao(PermResourceMappingDao permResourceMappingDao) {
        this.permResourceMappingDao = permResourceMappingDao;
    }

    @Autowired
    public void setResourceService(ResourceService resourceService) {
        this.resourceService = resourceService;
    }

    @Cacheable(keyGenerator = "customKeyGenerator")
    @Override
    public List<PermResourceMapping> findAll(PermResourceMapping prm) {
        List<PermResourceMapping> mappings = permResourceMappingDao.findAll(prm);
        mappings.forEach(it -> {
            it.setResource(this.resourceService.findById(it.getResourceId()));
        });
        return mappings;
    }

    @Cacheable(key = "#p0")
    @Override
    public PermResourceMapping findById(Integer id) {
        PermResourceMapping mapping = permResourceMappingDao.selectById(id);
        mapping.setResource(this.resourceService.findById(mapping.getResourceId()));
        return mapping;
    }

    @CacheEvict(allEntries = true)
    @Override
    public boolean save(PermResourceMapping rrm) {
        return permResourceMappingDao.insert(rrm) > 0;
    }

    @CacheEvict(allEntries = true)
    @Override
    public boolean update(PermResourceMapping rrm) {
        return permResourceMappingDao.updateById(rrm) > 0;
    }

    @CacheEvict(allEntries = true)
    @Override
    public int deleteByIds(List<Integer> ids) {
        return permResourceMappingDao.deleteByIds(ids);
    }
}
